package com.rg1906.filter;

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.rg1906.entity.LoginUser;



@WebFilter(filterName="/MyFilter",urlPatterns="/*")
public class MyFilter implements Filter {

   

	public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request=(HttpServletRequest)req;
		HttpServletResponse response=(HttpServletResponse)resp;
		request.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String requestURI = request.getRequestURI();
		if(requestURI.contains("/login.jsp")
				||requestURI.contains("/LoginServlet")
				||requestURI.contains("/goOut.jsp")
				||requestURI.endsWith(".js")
				||requestURI.endsWith(".png")
				||requestURI.endsWith(".jpg")
				||requestURI.endsWith(".css")
				) {
				
			chain.doFilter(request, response);
		}else {
			HttpSession session = request.getSession();
			LoginUser user = (LoginUser)session.getAttribute("user");
			if(user!=null) {
				chain.doFilter(request, response);
			}else {
				response.sendRedirect("./goOut.jsp");
			}
		}
		
	}

	

}
